package com.atguigu.educms.controller.admin;


import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.ResultCode;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-10-01
 */
@Api("轮播后台控制")
@RestController("adminCrmBannerController")
@RequestMapping("/educms/admin/crmBanner")
public class AdminCrmBannerController {

    @Resource
    private CrmBannerService bannerService;

    @ApiOperation("分页列表")
    @GetMapping("/pageInfo/{page}/{limit}")
    public R pageInfo(@PathVariable("page") Long page, @PathVariable("limit") Long limit){
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        return R.ok().data(pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    @ApiOperation("保存实体信息")
    @PostMapping("/saveEntity")
    public R saveEntity(
            @ApiParam(name = "CrmBanner", value = "轮播对象", required = true)
            @RequestBody CrmBanner entity) {
        bannerService.save(entity);
        return R.ok();
    }

    @ApiOperation("根据id删除实体信息")
    @DeleteMapping("/deleteEntityById/{id}")
    public R deleteEntityById(@PathVariable("id") String id) {
        CrmBanner entity = bannerService.getById(id);
        if(Objects.isNull(entity)){
            throw new GuLiException(ResultCode.ERROR,"该轮播对象不存在或已经删除");
        }else{
            return  bannerService.removeById(id) ? R.ok() :R.error();
        }
    }

    @ApiOperation("根据id获取实体信息")
    @GetMapping("/getEntityById/{id}")
    public R getEntityById(
            @ApiParam("轮播id")
            @PathVariable("id") String id
    ){
        return R.ok().data("info", bannerService.getById(id));
    }

    @ApiOperation("根据Id修改实体信息")
    @PutMapping("/updateEntity")
    public R updateEntity(
            @ApiParam(name = "CrmBanner", value = "轮播对象", required = true)
            @RequestBody CrmBanner entity) {
        bannerService.updateById(entity);
        return R.ok();
    }
}

